/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007 Neil J. Patel <njpatel@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Neil J. Patel <njpatel@gmail.com>
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <gtk/gtk.h>
#include <panel-applet.h>
#include <string.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomevfs/gnome-vfs.h>

#include "affinity.h"

#include "applet-button.h"

static void aff_panel_background_changed(PanelApplet               *applet,
				  PanelAppletBackgroundType  type,
				  GdkColor                  *colour,
				  GdkPixmap                 *pixmap,
                                  gpointer user_data);
static void display_properties_dialog (BonoboUIComponent *component, gpointer user_data, const gchar *verb);
static void display_about_dialog (BonoboUIComponent *component, gpointer user_data, const gchar *verb);

static const char Context_menu_xml [] =
   "<popup name=\"button3\">\n"
   "   <menuitem name=\"Properties Item\" "
   "             verb=\"MenuProperties\" "
   "           _label=\"_Preferences\"\n"
   "          pixtype=\"stock\" "
   "          pixname=\"gtk-properties\"/>\n"
   "   <menuitem name=\"About Item\" "
   "             verb=\"MenuAbout\" "
   "           _label=\"_About\"\n"
   "          pixtype=\"stock\" "
   "          pixname=\"gnome-stock-about\"/>\n"
   "</popup>\n";

static const BonoboUIVerb _menu_verbs [] = {
        BONOBO_UI_VERB ("MenuProperties", display_properties_dialog),
        BONOBO_UI_VERB ("MenuAbout", display_about_dialog),
        BONOBO_UI_VERB_END
}; 

static const gchar *affinity_authors [] = {
	"Neil J. Patel <njpatel@gmail.com>",
	NULL
};

static gboolean
Affinity_applet_fill( PanelApplet *applet, const gchar *iid, gpointer data )
{
	static int init = 0;
	if ( init )
		return FALSE;
	
	if ( strcmp( iid, "OAFIID:GNOME_Affinity") != 0 )
		return FALSE;
	
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        textdomain (GETTEXT_PACKAGE);
	
	/* SIGNALS */
	g_signal_connect( G_OBJECT( applet ), "change-background",
			  G_CALLBACK( aff_panel_background_changed ),
			  NULL);
	
	GtkWidget *menu = applet_button_new (applet);
	
	gtk_container_add (GTK_CONTAINER (applet), menu);

	gtk_widget_show_all (GTK_WIDGET (applet));
	
	panel_applet_setup_menu (PANEL_APPLET (applet),
                         	Context_menu_xml,
                         	_menu_verbs,
                         	NULL);	
	
	init = 1;
	return TRUE;
}

PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GNOME_Affinity_Factory",
                             PANEL_TYPE_APPLET,
                             "The Affinity Search Menu",
                             "0",
                             Affinity_applet_fill,
                             NULL);


static void
display_properties_dialog (BonoboUIComponent *component, gpointer user_data, const gchar *verb)
{
	gdk_spawn_command_line_on_screen (gdk_screen_get_default(),
                                          "affinity-preferences",
                                          NULL);
}

static void
display_about_dialog (BonoboUIComponent *component, gpointer user_data, const gchar *verb)
{
	static GtkWidget *panel_about_dialog = NULL;
	if (!panel_about_dialog) {
		panel_about_dialog = gtk_about_dialog_new ();

		g_object_set (panel_about_dialog,
			"name", _("Affinity Search Menu"),
			"comments", _("The Affinity Search Menu"),
			"version", "0.1",
			"authors", affinity_authors,
			"logo-icon-name", "search",
			"copyright", "Copyright \xc2\xa9 2007 Neil J. Patel",
			NULL);

		gtk_widget_show (panel_about_dialog);

		g_signal_connect (G_OBJECT (panel_about_dialog), "response",
			G_CALLBACK (gtk_widget_hide_on_delete), NULL);
	}

	gtk_window_present (GTK_WINDOW (panel_about_dialog));
}


static void 
aff_panel_background_changed(PanelApplet               *applet,
				PanelAppletBackgroundType  type,
				GdkColor                  *colour,
				GdkPixmap                 *pixmap,
                                gpointer user_data)
{
	GtkRcStyle *rc_style;
	GtkStyle *style;

	/* reset style */
	gtk_widget_set_style (GTK_WIDGET (applet), NULL);
	rc_style = gtk_rc_style_new ();
	gtk_widget_modify_style (GTK_WIDGET (applet), rc_style);
	gtk_rc_style_unref (rc_style);

	switch (type) {
	case PANEL_NO_BACKGROUND:
		break;
	case PANEL_COLOR_BACKGROUND:
		gtk_widget_modify_bg (GTK_WIDGET (applet),
				      GTK_STATE_NORMAL, colour);
		break;
	case PANEL_PIXMAP_BACKGROUND:
		style = gtk_style_copy (GTK_WIDGET (applet)->style);
		if (style->bg_pixmap[GTK_STATE_NORMAL])
			g_object_unref (style->bg_pixmap[GTK_STATE_NORMAL]);
		style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref (pixmap);
		gtk_widget_set_style (GTK_WIDGET (applet), style);
		g_object_unref (style);
		break;
	}
}
